package com.gin.no_gay_alliance.manager;

import androidx.annotation.NonNull;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.gin.jackson.utils.JacksonUtils;
import com.gin.nga.client.NgaClient;
import com.gin.nga.method.NukeApi;
import com.gin.nga.params.nuke.item.ItemBuyParam;
import com.gin.nga.params.nuke.item.ItemStoreParam;
import com.gin.nga.params.nuke.item.ItemUseParam;
import com.gin.nga.response.body.BaseMessageBody;
import com.gin.nga.response.body.nuke.ItemBody;
import com.gin.no_gay_alliance.app.MyApplication;
import com.gin.no_gay_alliance.callback.Bundle;
import com.gin.no_gay_alliance.callback.Callback;
import com.gin.no_gay_alliance.callback.RequestCallback;

import java.util.HashMap;
import java.util.Locale;
import java.util.concurrent.TimeUnit;

/**
 * 道具仓库管理器
 *
 * @author : ginstone
 * @version : v1.0.0
 * @since : 2023/9/7 13:37
 **/
public class ItemInventoryManager {
    /**
     * 缓存对象
     */
    private final HashMap<String, Bundle<ItemBody>> cache = new HashMap<>();

    /**
     * 查询道具仓库
     *
     * @param ignoreCache 是否忽略缓存
     * @param page        页数
     * @param callback    回调
     */
    public void itemInventory(boolean ignoreCache, int page, @NonNull Callback<ItemBody> callback) {
        NgaClient client = MyApplication.getClient();
        // 检查缓存
        String key = String.format(Locale.CHINA, "道具仓库_%d_%d", client.getUserId(), page);
        if (Bundle.checkCache(ignoreCache, cache, key, callback)) {
            return;
        }
        callback.beforeRequest();
        NukeApi.itemInventory(client, page).async(new RequestCallback<ItemBody>(callback) {
            @Override
            public void onSuccess(ItemBody body) {
                cache.put(key, new Bundle<>(body, 30, TimeUnit.MINUTES));
                super.onSuccess(body);
            }
        });
    }

    /**
     * 查询商店道具
     *
     * @param ignoreCache 是否忽略缓存
     * @param param       参数
     * @param callback    回调
     */
    public void itemStore(boolean ignoreCache, ItemStoreParam param, @NonNull Callback<ItemBody> callback) {
        NgaClient client = MyApplication.getClient();
        // 检查缓存
        String key = getKey(param);
        if (Bundle.checkCache(ignoreCache, cache, key, callback)) {
            return;
        }
        callback.beforeRequest();
        NukeApi.itemStore(client, param).async(new RequestCallback<ItemBody>(callback) {
            @Override
            public void onSuccess(ItemBody body) {
                cache.put(key, new Bundle<>(body, 30, TimeUnit.MINUTES));
                super.onSuccess(body);
            }
        });
    }

    /**
     * 购买道具
     *
     * @param param    道具参数
     * @param callback 回调
     */
    public void itemBuy(ItemBuyParam param, Callback<BaseMessageBody> callback) {
        NukeApi.itemBuy(MyApplication.getClient(), param).async(new RequestCallback<BaseMessageBody>(callback) {
            @Override
            public void onSuccess(BaseMessageBody body) {
                //清空缓存
                cache.clear();
                super.onSuccess(body);
            }
        });
    }

    /**
     * 使用道具
     *
     * @param param    道具参数
     * @param callback 回调
     */
    public void itemUse(ItemUseParam param, Callback<BaseMessageBody> callback) {
        NukeApi.itemUse(MyApplication.getClient(), param).async(new RequestCallback<BaseMessageBody>(callback) {
            @Override
            public void onSuccess(BaseMessageBody body) {
                //清空缓存
                cache.clear();
                super.onSuccess(body);
            }
        });
    }


    /**
     * 根据param生成key
     *
     * @param param 参数
     * @return key
     */
    private static String getKey(Object param) {
        try {
            String typeName = param.getClass().getSimpleName();
            String value = JacksonUtils.MAPPER.writeValueAsString(param);
            return (typeName + value).replace("\n", "").replace(" ", "");
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
    }
}
